--[[
    函数：lua3_lab_add_design
    功能：添加模版
    参数：ARGV[1]:design_id，模版id，
    参数：ARGV[2]:type_large，一级分类，
    参数：ARGV[3]:type_middle，二级分类
    参数：ARGV[4]:type_small，三级分类
    参数：ARGV[5]:industries，行业标签ID字符串,以半角逗号分割，形如：'1,2,3'
    参数：ARGV[6]:scenes,场景标签ID字符串,以半角逗号分割，形如：'1,2,3'
    参数：ARGV[7]:colors, 颜色标签ID字符串,以半角逗号分割，形如：'1,2,3'
    参数：ARGV[8]:styles, 风格标签ID字符串,以半角逗号分割，形如：'1,2,3'
    参数：ARGV[9]:customs,自定义标签ID字符串,以半角逗号分割，形如：'1,2,3'
    参数：ARGV[10]:info，搜索结果字符串
    参数：ARGV[11]:sort，排序字段，越大越靠前
    返回：0表示成功，1表示失败
--]]

-- ########################################################

-- 分类的前缀
local PREFIX_TYPE = "lab_type_"
-- 标签的前缀
local PREFIX_LABEL_INDUSTRY = "lab_label_industry_"
local PREFIX_LABEL_SCENE = "lab_label_scene_"
local PREFIX_LABEL_STYLE = "lab_label_style_"
local PREFIX_LABEL_COLOR = "lab_label_color_"
local PREFIX_LABEL_CUSTOM = "lab_label_custom_"

-- 排序的key
local KEY_SORT = "lab_sort"
-- ID-模版缓存的前缀
local PREFIX_DESIGN = "lab_d_"

-- key的连接符号
local PREFIX_S = "*"
-- 数组字符串的分割符号
local PREFIX_ARRAY_SPLIT = ","

-- ########################################################
-- 将数据插入到集合中
local insertToSet = function(argv_value, split_char, prefix,design_id)
    local values = string.split(argv_value, split_char)
    if(values~=nil)
    then
        local r_len = #values
        if(r_len>0)
        then
            for i= 1, r_len do
                local key = prefix..values[i]
                redis.call('SADD',key, design_id)
            end
        end
    end

end


-- ########################################################
-- 参数检测
local design_id = ARGV[1]
local type_large = tonumber(ARGV[2]) or 0
if(type_large<=0)
then
    return 1
end

-- 参数检测
local type_middle = tonumber(ARGV[3]) or 0
if(type_middle<=0)
then
    return 1
end

-- 参数检测
local type_small = tonumber(ARGV[4]) or 0
if(type_small<=0)
then
    return 1
end

-- ########################################################
-- 分类集合  D1
local keyTl = PREFIX_TYPE..type_large
redis.call('SADD',keyTl, design_id)

local keyTm = PREFIX_TYPE..type_middle
redis.call('SADD',keyTm, design_id)

local keyTs = PREFIX_TYPE..type_small
redis.call('SADD',keyTs, design_id)

-- ########################################################
-- “行业标签－模版缓存”集合 D2
insertToSet( ARGV[5],PREFIX_ARRAY_SPLIT, PREFIX_LABEL_INDUSTRY, design_id)
-- “场景标签－模版缓存”集合 D3
insertToSet( ARGV[6],PREFIX_ARRAY_SPLIT, PREFIX_LABEL_SCENE, design_id)
-- “颜色标签－模版缓存”集合 D4
insertToSet( ARGV[7],PREFIX_ARRAY_SPLIT, PREFIX_LABEL_COLOR, design_id)
-- “风格标签－模版缓存”集合 D5
insertToSet( ARGV[8],PREFIX_ARRAY_SPLIT, PREFIX_LABEL_STYLE, design_id)
-- “自定义标签－模版缓存”集合 D6
insertToSet( ARGV[9],PREFIX_ARRAY_SPLIT, PREFIX_LABEL_CUSTOM, design_id)

-- ########################################################
-- "id－模版缓存”hash表  D7
local key7 = PREFIX_DESIGN..design_id
redis.call('hmset',key7, 'search_result', ARGV[10])

-- ########################################################
-- “模版－排序字段”有序集合  D8
local key8 = KEY_SORT
local sort = tonumber(ARGV[11]) or 0
redis.call('zadd',key8, sort, design_id)

-- ########################################################
return 0